package com.bjsxt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.constants.Constants;
import com.bjsxt.domain.CareOrderItem;
import com.bjsxt.domain.CheckResult;
import com.bjsxt.domain.OrderChargeItem;
import com.bjsxt.dto.CheckResultDto;
import com.bjsxt.dto.CheckResultFormDto;
import com.bjsxt.mapper.CareOrderItemMapper;
import com.bjsxt.mapper.CheckResultMapper;
import com.bjsxt.mapper.OrderChargeItemMapper;
import com.bjsxt.service.CheckResultService;
import com.bjsxt.vo.DataGridView;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Method;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;

import java.util.List;

/**
 * 检查单service接口实现类
 */
@Service(methods = {@Method(name = "saveCheckResult", retries = 1)})
public class CheckResultServiceImpl implements CheckResultService {

    @Autowired
    private CareOrderItemMapper careOrderItemMapper;

    @Autowired
    private CheckResultMapper checkResultMapper;

    @Autowired
    private OrderChargeItemMapper orderChargeItemMapper;

    /**
     * 根据条件查询所有项目
     *
     * @param coType 项目类型 0 药品 1 检查
     * @param status 支付状态
     * @return
     */
    @Override
    public List<CareOrderItem> queryCareOrderItemsByStatus(String coType, String status) {
        QueryWrapper<CareOrderItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(coType), CareOrderItem.COL_ITEM_TYPE, coType);
        queryWrapper.eq(StringUtils.isNotBlank(status), CareOrderItem.COL_STATUS, status);
        return this.careOrderItemMapper.selectList(queryWrapper);
    }

    /**
     * 保存检查项目信息
     *
     * @param checkResult
     * @return
     */
    @Override
    public int saveCheckResult(@Validated CheckResult checkResult) {
//        保存检查单项目
        int i = this.checkResultMapper.insert(checkResult);
//        更新收费详情的状态
        OrderChargeItem orderChargeItem = new OrderChargeItem();
        orderChargeItem.setItemId(checkResult.getCocId());
        orderChargeItem.setStatus(Constants.ORDER_DETAILS_STATUS_3);  //已完成
        this.orderChargeItemMapper.updateById(orderChargeItem);
//        更新处方详情的状态
        CareOrderItem careOrderItem = new CareOrderItem();
        careOrderItem.setItemId(checkResult.getCocId());
        careOrderItem.setStatus(Constants.ORDER_DETAILS_STATUS_1);  //已完成
        this.careOrderItemMapper.updateById(careOrderItem);
        return i;
    }

    /**
     * 根据条件查询所有检查中和检查完成了的项目
     *
     * @param checkResultDto
     * @return
     */
    @Override
    public DataGridView queryAllCheckResultForPage(CheckResultDto checkResultDto) {
        Page<CheckResult> page = new Page<>(checkResultDto.getPageNum(), checkResultDto.getPageSize());
        QueryWrapper<CheckResult> queryWrapper = new QueryWrapper<>();
//        queryWrapper.in(checkResultDto.getCheckItemIds().size() > 0, CheckResult.COL_CHECK_ITEM_ID, checkResultDto.getCheckItemIds());
        queryWrapper.like(StringUtils.isNotBlank(checkResultDto.getPatientName()), CheckResult.COL_PATIENT_NAME, checkResultDto.getPatientName());
        queryWrapper.like(StringUtils.isNotBlank(checkResultDto.getRegId()), CheckResult.COL_REG_ID, checkResultDto.getRegId());
        queryWrapper.eq(StringUtils.isNotBlank(checkResultDto.getResultStatus()), CheckResult.COL_RESULT_STATUS, checkResultDto.getResultStatus());
        this.checkResultMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    /**
     * 完成检查
     *
     * @param checkResultFormDto
     * @return
     */
    @Override
    public int completeCheckResult(CheckResultFormDto checkResultFormDto) {
        CheckResult checkResult = new CheckResult();
        BeanUtil.copyProperties(checkResultFormDto, checkResult);
        checkResult.setResultStatus(Constants.RESULT_STATUS_1);  //修改检查状态为完成
        checkResult.setUpdateBy(checkResultFormDto.getSimpleUser().getUserName());
        return this.checkResultMapper.updateById(checkResult);
    }

}
